package com.guigu.userservice.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.R;
import com.guigu.userservice.entity.SellerQualification;
import com.guigu.userservice.entity.User;
import com.guigu.userservice.service.SellerQualificationService;
import com.guigu.userservice.service.UserService;
import com.guigu.userservice.vo.SellerQualificationVo;
import com.guigu.userservice.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 卖家资质表（存储卖家实名认证材料及审核结果） 前端控制器
 * </p>
 *
 * @author zcy
 * @since 2025-09-27
 */
@RequestMapping("/user/sellerQualification")
@RestController
public class SellerQualificationController {
    @Autowired
    private SellerQualificationService sellerQualificationService;
    @Autowired
    private UserService userService;
    /**
     * 根据条件查询卖家资质
     */
    @PostMapping("/querySellerQualificationByVo/{pageNum}/{pageSize}")
    public R querySellerQualificationByVo(@RequestBody SellerQualificationVo vo,
                                          @PathVariable("pageNum") Integer pageNum,
                                          @PathVariable("pageSize") Integer pageSize
    ){
        return sellerQualificationService.querySellerQualificationByVo(pageNum, pageSize,vo);
    }

    /**
     * 根据id查询卖家资质
     */
    @GetMapping("/querySellerQualificationDetail/{id}")
    public R querySellerQualificationDetail(@PathVariable("id") Integer id){
        return sellerQualificationService.querySellerQualificationDetail(id);
    }

    @PutMapping("/updateSellerQualification")
    public R updateSellerQualification(@RequestBody SellerQualification vo){
        if (vo.getQualificationId() == null){
            return R.error().message("参数错误");
        }
        vo.setAuditTime(LocalDateTime.now());
        sellerQualificationService.updateById(vo);
        if (vo.getAuditStatus() == 1){
            User user = new User();
            user.setUserId(vo.getUserId());
            user.setUserType(2);
            userService.updateById(user);
        }
        return R.ok();
    }

    /**
     * 前台-提交卖家身份证信息
     * */
    @PostMapping("/savasellerQualificationInfo")
    public R savasellerQualificationInfo(@RequestBody SellerQualification sellerQualification){

        sellerQualification.setAuditStatus(0);
        sellerQualification.setApplyTime(LocalDateTime.now());
        sellerQualification.setCreateTime(LocalDateTime.now());
        sellerQualification.setUpdateTime(LocalDateTime.now());
        boolean save = sellerQualificationService.save(sellerQualification);
        if (save==true)
            return R.ok().code(20000).message("ok").data("success", true);
        return R.error().code(20001).message("no").data("success", false);
    }
}
